﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.Admin
{
    public partial class ListEnterpriseCheck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["AdminLogin2"]  != null)
                {
                    int atid = Convert.ToInt32(Session["AdminLogin2"] );

                    Bind(atid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this,"alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Session["AdminLogin2"]  != null)
            {
                int atid = Convert.ToInt32(Session["AdminLogin2"] );

                Bind(atid);

                //ViewState["page"] = AspNetPager1.CurrentPageIndex;
            }
        }
       
        private void Bind(int atid)
        {
            //if (ViewState["page"] != null && ViewState["page"].ToString() != "")
            //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());

            int count = 0;
            lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

            DataSet ds = eb.GetCheckInfo(atid,this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            
            this.lbl_count.Text = ds.Tables[1].Rows[0][0].ToString();

            this.AspNetPager1.RecordCount = count;

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        //审核
        protected void lbn_check_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

                if (eb.UpdateIDList("status",Idlist,"1"))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "审核成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要审核的单位！", "ListEnterpriseCheck.aspx");
            }
            

        }

        //取消审核
        protected void lbn_cancle_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

                if (eb.UpdateIDList("status", Idlist, "2"))
                {
                    //Maticsoft.Common.MessageBox.Show(this, "取消审核成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要审核的单位！", "ListEnterpriseCheck.aspx");
            }
        }
        //发送邮件
        protected void lbn_sendMail_Click(object sender, EventArgs e)
        {
            string emaillist = this.GetEmailAddlist();
            if (emaillist.Length > 0)
            {
                Session["receiveEmailAdd"] = emaillist;
                Session["mailType"] = 1; //单位为1
                Response.Redirect("EmailSend.aspx");

            }
            else
            {
                //Maticsoft.Common.MessageBox.Show(this, "请选择所要发送邮件的单位！");
            }
        }
        //删除
        protected void lbn_del_Click(object sender, EventArgs e)
        {
            string Idlist = this.GetSelIDlist();

            if (!string.IsNullOrEmpty(Idlist))
            {
                string[] ids = Idlist.Split(',');

                bool flag = false;

                if (ids.Length>0)
                {
                    foreach (string id in ids)
                    {
                        int bid = Convert.ToInt32(id);

                        flag = DeleteBid(bid);
                    }

                }

                if (flag)
                {
                    //Maticsoft.Common.MessageBox.Show(this, "删除单位成功！");
                    if (Session["AdminLogin2"] != null)
                    {
                        int atid = Convert.ToInt32(Session["AdminLogin2"]);

                        Bind(atid);
                    }
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要审核的单位！", "ListEnterpriseCheck.aspx");
            }
        }
        /// <summary>
        /// 获取主键ID列表
        /// </summary>
        /// <returns></returns>
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        /// <summary>
        /// 获取Email邮件列表
        /// </summary>
        /// <returns></returns>
        private string GetEmailAddlist()
        {
            string emaillist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_email") as Label;
                    
                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        emaillist += lb.Text + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                emaillist = emaillist.Substring(0, emaillist.LastIndexOf(","));
            }
            return emaillist;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                if (e.CommandArgument != null)
                {
                    int bid = Convert.ToInt32(e.CommandArgument);

                    if (DeleteBid(bid))
                    {
                        //Maticsoft.Common.MessageBox.Show(this, "删除单位成功！");

                        if (Session["AdminLogin2"] != null)
                        {
                            int atid = Convert.ToInt32(Session["AdminLogin2"]);

                            Bind(atid);
                        }
                    } 

                }
            }
        }

        private bool DeleteBid(int bid)
        {
            bool re;
            lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

            lcmyjobs.BLL.enterprise_releasejob er = new BLL.enterprise_releasejob();

            DataSet ds = er.GetList("enterprise_bid=" + bid);

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    int rid = Convert.ToInt32(row["enterprise_rid"]);

                    er.Delete(rid);
                }

            }

            if (eb.Delete(bid))
            {
                re = true;
            }
            else
            {
                re = false;
            }

            return re;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[5].Text == "未审核")
            {
                e.Row.Cells[5].ControlStyle.ForeColor = System.Drawing.Color.Red;
            }
        }

       
    }
}